package com.atguigu.demojuc.chap02;

import java.util.concurrent.locks.ReentrantLock;

//测试死锁
public class DeadlockDemo {

    /*private static final Object lock1 = new Object();
    private static final Object lock2 = new Object();*/
    private static ReentrantLock lock1 = new ReentrantLock();
    private static ReentrantLock lock2 = new ReentrantLock();

    public static void main(String[] args) {

        /*new Thread(() -> {
            //线程1获取锁1
            synchronized (lock1) {
                System.out.println("Thread 1: Holding lock 1...");
                try {
                    Thread.sleep(500); // Simulate some work
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("Thread 1: Waiting for lock 2...");
                //线程1尝试获取锁2
                synchronized (lock2) {
                    System.out.println("Thread 1: Both locks acquired.");
                }
            }
        }).start();

        new Thread(() -> {
            //线程2获取锁2
            synchronized (lock2) {
                System.out.println("Thread 2: Holding lock 2...");
                try {
                    Thread.sleep(500); // Simulate some work
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("Thread 2: Waiting for lock 1...");
                //线程2尝试获取锁1
                synchronized (lock1) {
                    System.out.println("Thread 2: Both locks acquired.");
                }
            }
        }).start();*/



        //解决死锁问题
        //使用lock的方法：tryLock，尝试获取，返回值是boolean，判断是否获取到了，获取不到不执行下面方法，
        new Thread(() -> {

            boolean result1 = lock1.tryLock();
            if (result1){
                try {
                    System.out.println("Thread 1: Holding lock 1...");
                    try {
                        Thread.sleep(500); // Simulate some work
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    System.out.println("Thread 1: Waiting for lock 2...");

                    boolean result2 = lock2.tryLock();
                    if(result2){
                        try {
                            System.out.println("Thread 1: Both locks acquired.");
                        } finally {
                            lock2.unlock();
                        }
                    }else {
                        System.out.println("Thread 1: 未获取到锁2");
                    }

                }finally {
                    lock1.unlock();
                }
            }else{
                System.out.println("Thread 1: 未获取到锁1");
            }


        }).start();

        new Thread(() -> {

            boolean result2 = lock2.tryLock();

            if(result2){
                try {

                    System.out.println("Thread 2: Holding lock 2...");
                    try {
                        Thread.sleep(500); // Simulate some work
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("Thread 2: Waiting for lock 1...");


                    boolean result1 = lock1.tryLock();
                    if(result1){
                        try {
                            System.out.println("Thread 2: Both locks acquired.");
                        } finally {
                            lock1.unlock();
                        }
                    }else {
                        System.out.println("Thread 2: 未获取到锁1");
                    }
                }finally {
                    lock2.unlock();
                }
            }else{
                System.out.println("Thread 2: 未获取到锁2");
            }

        }).start();
    }
}